const crypto = require('crypto');

// 生成密钥和IV（初始化向量）
const algorithm = 'aes-256-cbc'; // 可以选择不同的算法，如 'aes-192-cbc' 或 'aes-128-cbc'
const key = crypto.randomBytes(32); // 对于aes-256-cbc，密钥长度应为32字节
const iv = crypto.randomBytes(16); // IV长度应为16字节

// 加密函数
function encrypt(text) {
  let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv);
  let encrypted = cipher.update(text);
  encrypted = Buffer.concat([encrypted, cipher.final()]);
  return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}

// 解密函数
function decrypt(iv, encryptedData) {
  let decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), Buffer.from(iv, 'hex'));
  let decrypted = decipher.update(Buffer.from(encryptedData, 'hex'));
  decrypted = Buffer.concat([decrypted, decipher.final()]);
  return decrypted.toString();
}
export {
    decrypt,
    encrypt
}
